package com.huaxiafinance.management.filter;

import com.huaxiafinance.management.util.MessageResult;
import com.huaxiafinance.management.util.ServiceCode;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
@Component
public class LoginFilter extends HandlerInterceptorAdapter {


	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		MessageResult messageResult = new MessageResult();
		String token = null;
		token=(String) request.getSession().getAttribute("token");
		if (token != null) {
			return true;
		} else {
			messageResult.setResultMessage(ServiceCode.USER_LOGIN_FAIL_TO_LOGIN);
			response.setHeader("loginFail", messageResult.getAsJSON());
			PrintWriter out =response.getWriter();
			out.write("{\"resultCode\":\"1008\",\"msg\":\"登陆超时\"}");
			out.flush();
			out.close();
			return false;
		}
	}
}
